package leecode

func countDigitOne(n int) int {
	factor := 1 // 10^1 10^2 10^3
	count := 1
	next := n / factor
	for next != 0 {
		// n = 2333 next = 2333 factor = 1
		// lower = 0
		// n = 2333 next = 233 factor = 10
		// lower = 3
		lower := n - next*factor
		// next = 2333 cur = 3 -- 个位的3
		// next = 233 cur = 3 -- 十位的3
		cur := next % 10
		// n = 2333 high = 233
		// n = 2333 high = 12
		high := n / (factor * 10)
		if cur > 1 {
			count += (high + 1) * factor
		} else if cur == 1 {
			count += high*factor + lower + 1
		} else {
			count += high * factor
		}
		factor = factor * 10
		next = n / factor
	}
	return count
}
